package com.chenglu.android.store;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chenglu.android.R;
import com.chenglu.android.common.base.BaseFullScreenActivity;
import com.chenglu.android.common.base.Global;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.utils.GlideUtils;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.StatusBarUtil;
import com.chenglu.android.goodsinfo.bean.ShopsinfoBean;
import com.chenglu.android.goodsinfo.infoutils.GoodsInfoUtils;
import com.chenglu.android.index.bean.IndexRecomBean;
import com.chenglu.android.search.SearchHistoryActivity;
import com.chenglu.android.store.adapter.BangAdapter;
import com.chenglu.android.store.bean.ChartBangBean;
import com.chenglu.android.store.mvp.contract.StoreContract;
import com.chenglu.android.store.mvp.presenter.StorePresenter;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: lyj
 * @Created: by Copyright 2023/10/24 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description： 商超的类似的店铺页
 */
public class StoreActivityNew extends BaseFullScreenActivity<StorePresenter> implements StoreContract.View {

    public static final String TENANTID = "tenantId";
    public static final String SHOPSTYPEID = "SHOPSTYPEID";
    @BindView(R.id.ic_back)
    ImageView icBack;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.iv_shop_header)
    ImageView ivShopHeader;
    @BindView(R.id.cv_user)
    CardView cvUser;
    @BindView(R.id.tv_shopname)
    TextView tvShopname;
    @BindView(R.id.ratingBar_service)
    RatingBar ratingBarService;
    @BindView(R.id.rt_shop_des)
    RelativeLayout rtShopDes;
    @BindView(R.id.tv_sales_all)
    TextView tvSalesAll;
    @BindView(R.id.tv_sales_chart)
    TextView tvSalesChart;
    @BindView(R.id.tv_collect_chart)
    TextView tvCollectChart;
    @BindView(R.id.tv_new_chart)
    TextView tvNewChart;
    @BindView(R.id.ll_bang_bg)
    LinearLayout llBangBg;
    @BindView(R.id.icon_search)
    ImageView iconSearch;
    @BindView(R.id.rv_bang_store)
    RecyclerView rvBangStore;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    @BindView(R.id.ll_loading_data)
    LinearLayout llLoadingData;
    @BindView(R.id.ll_loading_no_data)
    LinearLayout llLoadingNoData;



    private BangAdapter bangAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_new);
        ButterKnife.bind(this);
        StatusBarUtil.immersive(this);
        StatusBarUtil.setPaddingSmart(this, toolbar);

        tenantId = getIntent().getStringExtra(TENANTID);
        shopsTypeId = getIntent().getIntExtra(SHOPSTYPEID,0);
        mPresenter = new StorePresenter();
        mPresenter.attachView(this);


        //榜单切换
        initBangAdapter();

        initData();

    }

    private void initBangAdapter() {
        //排序有问题
        bangAdapter = new BangAdapter(mContext, list);

        rvBangStore.setLayoutManager(new LinearLayoutManager(this));

        rvBangStore.setAdapter(bangAdapter);
//        searchAdapter.notifyDataSetChanged();
        bangAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                GoodsInfoUtils.goodsInfo(mContext, bangAdapter.getData().get(position).getId(),shopsTypeId);
//
            }
        });

    }

    private String tenantId;
    private int bangStatus = 1;
    private int current = 1;
    private int shopsTypeId ;

    private String type = "0";

    //请求数据
    private void initData() {

        initRefreshLayout();

        mPresenter.getShopinfo(tenantId);
//         mPresenter.getShopsTypes();

//        mPresenter.getShopsTypes(getToken(), bangStatus, tenantId);

         initMapParam(current,Constant.SIZETEN,type);


    }

    private void initMapParam(int current, int size, String type) {

        Map<String, String> productMap = new HashMap<>();

//        productMap.put("categoryId",categoryId);
        productMap.put("current", current + "");
        productMap.put("size", size + "");   //
        productMap.put("type", type);   //
//        productMap.put("tenantId", tenantId);   //


        mPresenter.getShopsRecommendPage(tenantId,productMap);
    }

    List<IndexRecomBean.Data.Records> list = new ArrayList<>();


    //刷新
    private void initRefreshLayout() {

        smartrefreshlayout.setEnableRefresh(false);
        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //id 需要传参过来
                current = 1;
                //需要清空数据再加
                list.clear();
                initMapParam(current,Constant.SIZETEN,type);



//                setSearchData(categoryId, currentSearchName, current, Constant.SIZE);

            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                current += 1;
                initMapParam(current,Constant.SIZETEN,type);


            }
        });
    }

    boolean isAddFooterView = true;


    @Override
    public void getShopinfo(ShopsinfoBean bean) {
        tvShopname.setText(bean.getData().getShopsName());
        ratingBarService.setRating(bean.getData().getScore());
//        ratingBarService.setNumStars(bean.getData().getScore());
        GlideUtils.setImageView(mContext, bean.getData().getLogoImage(), ivShopHeader);
    }

    @Override
    public void getShopsTypes(ChartBangBean bean) {

    }

    @Override
    public void getLikeLists(IndexRecomBean bean) {

    }

    @Override
    public void getSearchGoods(IndexRecomBean bean) {

    }

    @Override
    public void getShopsRecommendPage(IndexRecomBean bean) {
        stopRefresh(smartrefreshlayout);

        if (bean.getData().getRecords().size() > 0) {
            llLoadingData.setVisibility(View.VISIBLE);
            llLoadingNoData.setVisibility(View.GONE);
            for (int i = 0; i < bean.getData().getRecords().size(); i++) {
                //添加集合中banner的数据
                list.add(bean.getData().getRecords().get(i));
            }
            if (current * Constant.SIZETEN > bean.getData().getTotal()) {
                smartrefreshlayout.setEnableLoadMore(false);

                if (isAddFooterView) {
                    View inflate = Global.inflate(R.layout.loadmore_addfooter_layout);
                    inflate.findViewById(R.id.tv_no_more).setBackgroundColor(getResources().getColor(R.color.white));
                    bangAdapter.addFooterView(inflate);
                    isAddFooterView = false;
                }

            } else {
                isAddFooterView = true;
                bangAdapter.removeAllFooterView();
                smartrefreshlayout.setEnableLoadMore(true);

            }
            bangAdapter.notifyDataSetChanged();
        } else {
            if (list.isEmpty()) {
//                showEmptyView("暂无数据");
                llLoadingData.setVisibility(View.GONE);
                llLoadingNoData.setBackground(getResources().getDrawable(R.drawable.corners_white_shape));
                llLoadingNoData.setVisibility(View.VISIBLE);
            }

        }
    }



    @OnClick({R.id.ll_back, R.id.rt_shop_des, R.id.tv_sales_all, R.id.tv_sales_chart, R.id.tv_collect_chart, R.id.tv_new_chart, R.id.icon_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.rt_shop_des:
                startActivity(new Intent(this, StoreInfoActivity.class)
                        .putExtra("tenandId",tenantId));
                break;
            case R.id.tv_sales_all:
                current= 1;
                list.clear();
                type = "0";
                initMapParam(current,Constant.SIZE,type);
                tvSalesAll.setTypeface(null, Typeface.BOLD);
                tvSalesAll.setTextSize(16);
                tvSalesAll.setTextColor(getResources().getColor(R.color.all_black_text));
                tvSalesChart.setTypeface(null, Typeface.NORMAL);
                tvSalesChart.setTextSize(14);
                tvSalesChart.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvCollectChart.setTypeface(null, Typeface.NORMAL);
                tvCollectChart.setTextSize(14);
                tvCollectChart.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvNewChart.setTypeface(null, Typeface.NORMAL);
                tvNewChart.setTextSize(14);
                tvNewChart.setTextColor(getResources().getColor(R.color.all_grey_text));

                break;
            case R.id.tv_sales_chart:
                current= 1;
                list.clear();
                type = "1";
                initMapParam(current,Constant.SIZE,type);
                tvSalesAll.setTypeface(null, Typeface.NORMAL);
                tvSalesAll.setTextSize(14);
                tvSalesAll.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvSalesChart.setTypeface(null, Typeface.BOLD);
                tvSalesChart.setTextSize(16);
                tvSalesChart.setTextColor(getResources().getColor(R.color.all_black_text));
                tvCollectChart.setTypeface(null, Typeface.NORMAL);
                tvCollectChart.setTextSize(14);
                tvCollectChart.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvNewChart.setTypeface(null, Typeface.NORMAL);
                tvNewChart.setTextSize(14);
                tvNewChart.setTextColor(getResources().getColor(R.color.all_grey_text));
                break;
            case R.id.tv_collect_chart:
                current= 1;
                list.clear();
                type = "2";
                initMapParam(current,Constant.SIZE,type);
                tvSalesAll.setTypeface(null, Typeface.NORMAL);
                tvSalesAll.setTextSize(14);
                tvSalesAll.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvSalesChart.setTypeface(null, Typeface.NORMAL);
                tvSalesChart.setTextSize(14);
                tvSalesChart.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvCollectChart.setTypeface(null, Typeface.BOLD);
                tvCollectChart.setTextSize(16);
                tvCollectChart.setTextColor(getResources().getColor(R.color.all_black_text));
                tvNewChart.setTypeface(null, Typeface.NORMAL);
                tvNewChart.setTextSize(14);
                tvNewChart.setTextColor(getResources().getColor(R.color.all_grey_text));
                break;
            case R.id.tv_new_chart:
                list.clear();
                type = "3";
                initMapParam(current,Constant.SIZE,type);
                tvSalesAll.setTypeface(null, Typeface.NORMAL);
                tvSalesAll.setTextSize(14);
                tvSalesAll.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvSalesChart.setTypeface(null, Typeface.NORMAL);
                tvSalesChart.setTextSize(14);
                tvSalesChart.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvCollectChart.setTypeface(null, Typeface.NORMAL);
                tvCollectChart.setTextSize(14);
                tvCollectChart.setTextColor(getResources().getColor(R.color.all_grey_text));
                tvNewChart.setTypeface(null, Typeface.BOLD);
                tvNewChart.setTextSize(16);
                tvNewChart.setTextColor(getResources().getColor(R.color.all_black_text));
                break;
            case R.id.icon_search:
                startActivity(new Intent(this, SearchHistoryActivity.class));
                break;
        }
    }
}
